package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.OrgUserStuUtil;
import com.xyht.sca_s.student_manage_system.modules.student.entity.SmsStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.student.mapper.SmsStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsStudentLoanAmountRecord;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.SmsWnsStudentLoanAmountRecordResp;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsStudentLoanAmountRecordMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsStudentLoanAmountRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isLegalDateRange;
import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.constant.StudentLoanAmountRecordConstant.STUDENT_LOAN_AMOUNT_RECORD_TYPE_LIST;

/**
 * <p>
 * 学生助学贷款金额变动记录表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-04-09
 */
@Service
public class SmsWnsStudentLoanAmountRecordServiceImpl extends ServiceImpl<SmsWnsStudentLoanAmountRecordMapper, SmsWnsStudentLoanAmountRecord> implements SmsWnsStudentLoanAmountRecordService {
    @Resource
    private OrgUserStuUtil orgUserStuUtil;
    @Resource
    private SmsStudentInfoMapper smsStudentInfoMapper;

    @Override
    public ResponseResult list(Integer pageNum, Integer pageSize, Integer type, Integer orgId, Date startTime, Date endTime, String searchStr) {
        LambdaQueryWrapper<SmsWnsStudentLoanAmountRecord> queryWrapper = new LambdaQueryWrapper<SmsWnsStudentLoanAmountRecord>()
                .orderByDesc(SmsWnsStudentLoanAmountRecord::getCreateTime);

        if (type != null && STUDENT_LOAN_AMOUNT_RECORD_TYPE_LIST.contains(type)) {
            queryWrapper.eq(SmsWnsStudentLoanAmountRecord::getType, type);
        }

        if (orgId != null) {
            List<String> userIdList = orgUserStuUtil.getUserIdListByOrgId(orgId);
            queryWrapper.in(SmsWnsStudentLoanAmountRecord::getUserId, userIdList.isEmpty() ? Collections.singletonList("") : userIdList);
        }

        if (isLegalDateRange(startTime, endTime)) {
            queryWrapper.between(SmsWnsStudentLoanAmountRecord::getCreateTime, startTime, endTime);
        }

        if (!isNullOrEmpty(searchStr)) {
            List<String> stuIdList = orgUserStuUtil.getStuIdListBySearchStr(searchStr);
            queryWrapper.and(wrapper -> wrapper.like(SmsWnsStudentLoanAmountRecord::getNote, searchStr).or()
                    .in(SmsWnsStudentLoanAmountRecord::getStuId, stuIdList.isEmpty() ? Collections.singletonList("") : stuIdList));
        }

        Page<SmsWnsStudentLoanAmountRecord> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        List<SmsWnsStudentLoanAmountRecordResp> collect = page.getRecords().stream().map(record -> {
            SmsWnsStudentLoanAmountRecordResp recordResp = new SmsWnsStudentLoanAmountRecordResp();
            BeanUtils.copyProperties(record, recordResp);
            if (!isNullOrEmpty(record.getStuId())) {
                SmsStudentInfo studentInfo = smsStudentInfoMapper.selectById(record.getStuId());
                if (studentInfo != null) {
                    recordResp.setStudentInfo(studentInfo);
                }
            }

            return recordResp;
        }).collect(Collectors.toList());

        return CommonResult.success(collect, (int) page.getTotal());
    }
}
